﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.IO;
namespace Postproc_directX
{
    public partial class Form1 : Form
    {
        private DirectX directx=new DirectX();
        private Device device = null;
        private List<List<int>> nvtr;
        private List<PointF> XY;
        private List<float> X;
        private List<float> Y;
        private List<float> u;
        

        public Form1()
        {
            InitializeComponent();
            // Устраняем мерцание окна при бесконечной перерисовке
            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                ControlStyles.Opaque, true);
            //this.MouseWheel += new MouseEventHandler(PaintArea_MouseWheel);
            panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
            panel1.MouseClick += new MouseEventHandler(panel1_MouseClick);
            panel1.Focus();
        }

        void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            Vector3 v3 = new Vector3(38.617f, -2.5f, 1),res2;
            Vector2 res;
            res = directx.WorldSpaceToScreenSpace(v3);
            res2 = directx.ScreenSpaceToWorldSpace(res);

        }

        void panel1_MouseWheel(object sender, MouseEventArgs e)
        {
            float zoom1 = directx.zoom;
            if (e.Delta > 0)
            {
                directx.zoom = directx.zoom + 0.05f * zoom1;
                if (directx.zoom > 1000)
                    directx.zoom = 1000;
            }
            else
            {
                directx.zoom = directx.zoom - 0.05f * zoom1;
                if (directx.zoom < 0.001f)
                    directx.zoom = 0.001f;
                if (directx.zoom <= 0) directx.zoom = 0;
            }
            //Result.X = -0.5*DrawArea.Width  + ScreenX;
            //Result.Y = (0.5*DrawArea.Height - ScreenY)*RenderWidth/RenderHeight/CurrentAspectRatio;
            //directx.dx = -(e.X - (e.X - directx.dx) / zoom1 * directx.zoom);
            //directx.dy = -(DrawArea.Height - e.Y) - ((DrawArea.Height - e.Y) - directx.dy) / zoom1 * directx.zoom;
            DrawArea.Invalidate();
        }


        private void DrawArea_Paint(object sender, PaintEventArgs e)
        {
            directx.OnPaint();
        }


        void LoadData()
        {
            List<int> nvt = new List<int>(3);
            BinaryReader br = new BinaryReader(File.OpenRead("nvtr_treug"));
            StreamReader br1 = new StreamReader(File.OpenRead("pref"));

            int m = Convert.ToInt32(br1.ReadLine());
            int n = Convert.ToInt32(br1.ReadLine());
            nvtr = new List<List<int>>();
            XY = new List<PointF>();
            u = new List<float>();
            X = new List<float>();
            Y = new List<float>();

            br1.Close();
            for (int i = 0; i < m; i++)
            {
                nvt = new List<int>();
                nvt.Clear();
                nvt.Add(br.ReadInt32());
                nvt.Add(br.ReadInt32());
                nvt.Add(br.ReadInt32());
                br.ReadInt32();
                nvtr.Add(nvt);
            }
            br.Close();



            br = new BinaryReader(File.OpenRead("xy"));
            for (int i = 0; i < n; i++)
            {
                PointF p = new PointF();
                p.X = (float)br.ReadDouble();
                p.Y = (float)br.ReadDouble();
                XY.Add(p);
                X.Add(p.X);
                Y.Add(p.Y);

            }
            br.Close();
            try
            {
                br = new BinaryReader(File.OpenRead("q"));
                for (int i = 0; i < n; i++)
                {



                    u.Add((float)br.ReadDouble());

                }
            }
            catch
            {};
        }
    
        private void Form1_Shown(object sender, EventArgs e)
        {
            //directx = new DirectX(device, DrawArea.Width, DrawArea.Height);
            directx.InitializeGraphics(panel1);
            LoadData();
            directx.SetData(nvtr, X,Y, u);
        }
        private void PaintArea_MouseWheel(object sender, MouseEventArgs e)
        {
            float zoom1 = directx.zoom;
            if (e.Delta > 0)
            {
                directx.zoom = directx.zoom + 0.05f * zoom1;
                if (directx.zoom > 1000)
                    directx.zoom = 1000;
            }
            else
            {
                directx.zoom = directx.zoom - 0.05f * zoom1;
                if (directx.zoom < 0.001f)
                    directx.zoom = 0.001f;
                if (directx.zoom <= 0) directx.zoom = 0;
            }
            //Result.X = -0.5*DrawArea.Width  + ScreenX;
            //Result.Y = (0.5*DrawArea.Height - ScreenY)*RenderWidth/RenderHeight/CurrentAspectRatio;
            //directx.dx = -(e.X - (e.X - directx.dx) / zoom1 * directx.zoom);
            //directx.dy = -(DrawArea.Height - e.Y) - ((DrawArea.Height - e.Y) - directx.dy) / zoom1 * directx.zoom;
            DrawArea.Invalidate();
        }
        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
            {
                directx.width = DrawArea.Width;
                directx.height = DrawArea.Height;
                DrawArea.Invalidate();
            }
            else return;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Up)
            {
                directx.dy += 1;
                DrawArea.Invalidate();
            }
            if (e.KeyData == Keys.Down)
            {
                directx.dy -= 1;
                DrawArea.Invalidate();
            }
            if (e.KeyData == Keys.Left)
            {
                directx.dx -= 1;
                DrawArea.Invalidate();
            }
            if (e.KeyData == Keys.Right)
            {
                directx.dx += 1;
                DrawArea.Invalidate();
            }
        }

        private void panel1_MouseEnter(object sender, EventArgs e)
        {
            panel1.Focus();
        }

        private void panel1_MouseLeave(object sender, EventArgs e)
        {
            //panel1.
        }



    }
}
